﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace Teste
{
    public interface IArticle
    {
        string Title { get; set; }
        string[] Authors { get; set; }
        int YearOfPublication { get; set; }
        string Resume { get; set; }
    }

    [Serializable]
    public class Article : IArticle
    {
        [XmlElement("Title")]
        public string Title{ get; set; }

        [XmlArray("Authors")]
        [XmlArrayItem("Author")]
        public string[] Authors { get; set; }
        
        [XmlElement("Year")]
        public int YearOfPublication { get; set; }
        
        [XmlElement("Resume")]
        public string Resume { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var files = new List<List<Article>>();

            var rand = new Random();

            //Gerar 10 ficheiros
            for(var i = 0; i < 10; ++i)
            {
                var nArticles = rand.Next(1, 11);
                var articles = new List<Article>();

                //Gerar n (max 11) artigos
                for(var j = 0; j < nArticles; ++j)
                {
                    var nAuthors = rand.Next(1, 4);

                    var authors = new string[nAuthors];

                    for (var k = 0; k < nAuthors; ++k )
                        authors[k] = "Autor " + (k+1);

                        articles.Add(new Article
                                         {
                                             Authors = authors,
                                             Resume = "Resumo do artigo " + (j+1) + " da lista " + (i+1) + ".",
                                             Title = "Titulo do artigo " + (j + 1) + " da lista " + (i + 1) + ".",
                                             YearOfPublication = rand.Next(1990, 2012)
                                         });
                }

                files.Add(articles);
            }

            var serializerObj = new XmlSerializer(typeof(List<Article>));
            var count = 0;
            foreach (var file in files)
            {
                ++count;

                var path = @"..\..\Lista Artigos " + (count) + @".xml";
                TextWriter writeFileStream = new StreamWriter(path);
                serializerObj.Serialize(writeFileStream, file);

                writeFileStream.Close();
            }
        }
    }
}
